<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
<script src="//code.jquery.com/jquery-1.10.2.js"></script>
<script type="text/javascript">
$(function() {
	resetForm();
	$('#logoFormDiv').hide();
	
	$('#findUniv').keyup(function() {
		searchuniv();
	});
	
	$(document).on('click', '.univ', function() {
		resetForm();
		$('#edit_command').val('updateUniv');
		showLogo($(this).find('.univ_logo').text());
		$('#univ_no').val($(this).attr('name'));
		$('#univ_logo').val($(this).find('.univ_logo').text());
		$('#univ_name').val($(this).find('.univ_name').text());
		$('#univ_addr').val($(this).find('.univ_addr').text());
		$('#univ_president').val($(this).find('.univ_president').text());
		$('#univ_intro').val($(this).find('.univ_intro').text());
		$('#univ_home').val($(this).find('.univ_home').text());
		$('#univ_domitory').val($(this).find('.univ_domitory').text());
		$('#univ_establish').val($(this).find('.univ_establish').text());
		$('#univ_professor').val($(this).find('.univ_professor').text());
		$('#univ_student').val($(this).find('.univ_student').text());
		$('#region_no').find($('option[value='+$(this).find('.region_no').text()+']')).prop('selected', true);
		$('#univtype_no').find($('option[value='+$(this).find('.univtype_no').text()+']')).prop('selected', true);
		$('#submitBtn').html('수정');
		$('#resetBtn').show();
		$('#delBtn').show();
		$('#findUniv').val('');
		$('#univlist *').remove();
	});
	
	$('#submitBtn').click(function() {
		$('#editForm').submit();
	});
	
	$('#delBtn').click(function() {
		location.href="major.do?command=deleteUniv&&univ_no="+$('#univ_no').val();
	});
	
	$('#resetBtn').click(function() {
		resetForm();
	});
	
	$('#logoImgDiv').click(function() {
		$('#uploadFile').trigger('click');
	});
	
	$('#uploadFile').change(function() {
		
		 var formData = new FormData($('#logoForm').get(0));
		 
		if($('#univ_logo').val()==null||$('#univ_logo').val()==""){
		    formData.append('command', 'registerLogo');
		}else{
			formData.append('oldFileName', $('#univ_logo').val());
		    formData.append('command', 'updateLogo');
		}
		 
		$.ajax({
			type:"post",
			url:"major.do",
			data:formData,
            enctype: "multipart/form-data",
            dataType: "json",
            processData: false,
            contentType: false,
			
			success:function(data){
				showLogo(data.logofile);
			}
		});
	});
	
	$('#logoDel').click(function() {
		$.ajax({
			type:"post",
			url:"major.do",
			data:"command=deleteLogo&&oldFileName="+$('#univ_logo').val(),
			
			success:function(data){
				showLogo('');
				$('#univ_logo').val('');
			}
		});
	});
	
});

function checkNull(str) {
	if(str==null) return "";
	else return str;
}

function searchuniv() {
	univhtml="";
	$.ajax({
		type:"post",
		url:"major.do",
		data:"command=getUnivList&&search="+$('#findUniv').val(),
		dataType: "json",
		
		success:function(data){
			var list=data.univlist;
			for (var i=0;i<list.length;i++) {
				univhtml+=
					"<div class='univ' name='"+list[i].univ_no+"'>"+
						"<div class='univ_name'>"+list[i].univ_name+"</div>"+
						"<div class='hideDiv'>"+
							"<div class='univ_logo'>"+checkNull(list[i].univ_logo)+"</div>"+
							"<div class='univ_addr'>"+checkNull(list[i].univ_addr)+"</div>"+
							"<div class='univ_president'>"+checkNull(list[i].univ_president)+"</div>"+
							"<div class='univ_intro'>"+checkNull(list[i].univ_intro)+"</div>"+
							"<div class='univ_home'>"+checkNull(list[i].univ_home)+"</div>"+
							"<div class='univ_domitory'>"+checkNull(list[i].univ_domitory)+"</div>"+
							"<div class='univ_establish'>"+checkNull(list[i].univ_establish)+"</div>"+
							"<div class='univ_professor'>"+checkNull(list[i].univ_professor)+"</div>"+
							"<div class='univ_student'>"+checkNull(list[i].univ_student)+"</div>"+
							"<div class='region_no'>"+list[i].region.region_no+"</div>"+
							"<div class='univtype_no'>"+list[i].univtype.univtype_no+"</div>"+
						"</div>"+
					"</div>";
			}
			$('#univlist').html(univhtml);
			$('.hideDiv').hide();
		}
	});
}

function showLogo(univ_logo) {
	if(univ_logo==null || univ_logo==""){
		$('#univ_logo').val('');
		$('#logoImgDiv').html('<img alt="로고가 등록되어 있지 않습니다" src="../img/logo/no_image.jpg">');
	}else{
		$('#univ_logo').val(univ_logo);
		$('#logoImgDiv').html('<img src="../img/logo/'+univ_logo+'">');
	}
}

function resetForm() {
	$('#edit_command').val('registerUniv');
	$('#logo_command').val('');
	showLogo('');
	$('#univ_no').val('0');
	$('#univ_logo').val('');
	$('#univ_name').val('');
	$('#univ_addr').val('');
	$('#univ_president').val('');
	$('#univ_intro').val('');
	$('#univ_home').val('');
	$('#univ_domitory').val('');
	$('#univ_establish').val('');
	$('#univ_professor').val('');
	$('#univ_student').val('');
	$('#region_no').find($('option[value="0"]')).prop('selected', true);
	$('#univtype_no').find($('option[value="0"]')).prop('selected', true);
	$('#submitBtn').html('등록');
	$('#resetBtn').hide();
	$('#delBtn').hide();
}
</script>
</head>
<body>
<a href="index.do?command=menu">메뉴로</a>
<div id="findDiv">
	<input id="findUniv">
	<div id="univlist"></div>
</div>
<div id="editDiv">
	<div id="logoDiv">
		<div id="logoImgDiv"></div>
		<div id="logoFormDiv">
			<form action="major.do" id="logoForm">
				<input type="file" id="uploadFile" name="uploadFile">
			</form>
		</div>
		<div id="logoDel">로고 삭제</div>
	</div>
	<div id="formDiv">
		<form action="major.do" method="post" id="editForm">
			<input type="hidden" id="edit_command" name="command" value="registerUniv">
			<input type="hidden" id="univ_no" name="univ_no" value="0">
			<input type="hidden" id="univ_logo" name="univ_logo" value="">
			대학 이름 <input id="univ_name" name="univ_name"><br>
			주소 <input id="univ_addr" name="univ_addr"><br>
			총장 <input id="univ_president" name="univ_president"><br>
			홈페이지 <input id="univ_home" name="univ_home"><br>
			기숙사 규모 <input id="univ_domitory" name="univ_domitory"><br>
			설립일 <input id="univ_establish" name="univ_establish"><br>
			교원 수 <input id="univ_professor" name="univ_professor"><br>
			학생 수 <input id="univ_student" name="univ_student"><br>
			소개 <br>
			<textarea rows="10" cols="50" id="univ_intro" name="univ_intro"></textarea><br>
			
			<select name="region.region_no" id="region_no">
				<option value="0">지역</option>
				<c:forEach var="region" items="${requestScope.regionlist}">
					<option value="${region.region_no}">${region.region_name}</option>
				</c:forEach>
			</select><br>
			<select name="univtype.univtype_no" id="univtype_no">
				<option value="0">종류</option>
				<c:forEach var="univtype" items="${requestScope.univtypelist}">
					<option value="${univtype.univtype_no}">${univtype.univtype_name}</option>
				</c:forEach>
			</select>
		</form>
		<div id="submitBtn">등록</div>
		<div id="delBtn">삭제</div>
		<div id="resetBtn">새 단과대학 등록</div>
	</div>
</div>
</body>
</html>